fin = open('nuevos_genomas')
genome_list = fin.read().strip().split('\n')
fin.close()
genome_list = [g.replace('.fasta', '') for g in genome_list]
genome_intervals = {}
for genome in genome_list:
    fin = open('preprocesamiento_'+genome+'.fasta')
    data = fin.read().strip().split('\n')
    fin.close()
    cod_frec = {}
    for d in data:
        d = d.strip('\t').split('\t')
        for cod in d[1:]:
            try:
                cod_frec[cod.split(':')[0]].append(float(cod.split(':')[1]))
            except:
                cod_frec[cod.split(':')[0]]=[float(cod.split(':')[1])]
    intervals = {}
    #fout = open(genome +'.intervals', 'w')
    for cod, val in cod_frec.items():
        intervals[cod] = (max(val))
    #    intervals[cod] = (min(val), max(val), sum(val)/len(val))
    #    fout.write('{0}\t{1}\t{2}\n'.format(cod, min(val), max(val)))
    genome_intervals[genome] = intervals 
    #fout.close()
#fout = open('intervalos_todos', 'w')
#for genome,intervals in genome_intervals.items():
#    fout.write('{0}\t{1}\n{0}\t'.format(genome, '\t\t\t'.join(intervals.keys())))
#    for codon, val in intervals.items():
#        fout.write('{0}\t{1}\t{2}\t'.format(val[0], val[1],val[2]))
#        fout.write('{0}\t'.format(val[2]))
#    fout.write('\n') 
#fout.close()
fout= open('maximos', 'w')
codon_maxim = {}
for genome,intervals in genome_intervals.items():
    for codon, val in intervals.items():
        try:
            if val>codon_maxim[codon]:
                 codon_maxim[codon]=val
        except:
            codon_maxim[codon]=val

for codon, maxim in codon_maxim.items():
        fout.write('{0}\t{1}\n'.format(codon,maxim))

fout.close()
